/**
 * 
 */
package com.jquery.ui.core.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;


import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.DynamicAttributes;

import com.jquery.ui.core.bean.UIBean;
import com.jquery.ui.core.exception.JqueryUIRuntime;
import com.jquery.ui.core.freemarker.FreemarkerFactory;

/**
 * @author linyajun
 * 
 * @date 2013-9-18
 * @description
 */
public abstract class BaseBodyTag extends BodyTagSupport implements
		DynamicAttributes {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7946452631610044202L;
	private String id;
	private String renderTo;
	
	private JspWriter out;
	protected UIBean ui;

	private Map<String,Object> dynamicAttribute=new HashMap<String, Object>();
	private void init() {
		if (!isValid()) {
			throw new JqueryUIRuntime("The relationship between father and son error! please check!");
		}

		
		
		if (ui == null) {
			ui = getUiBean();
			ui.putAttriValue("id", id);
			ui.putAttriValue("renderTo", renderTo);
			ui.putAttriValue("dynamicAttribute", dynamicAttribute);
		}
		out = pageContext.getOut();
	}


	@Override
	public int doStartTag() throws JspException {
		init();
		if (getStartTemplate() != null) {
			FreemarkerFactory.getInstance().renderTemplate(getStartTemplate(),
					ui.getAttribute(), out);
		}

		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {
		if (bodyContent != null) {
			try {
				bodyContent.writeOut(bodyContent.getEnclosingWriter());
			} catch (IOException e) {

				e.printStackTrace();
			}
		}
		if (getEndTemplate() != null) {
			endInvoke(ui.getAttribute());
			FreemarkerFactory.getInstance().renderTemplate(getEndTemplate(),ui.getAttribute(),
					out);
		}
		endInvoke();
		ui = null;
		return EVAL_PAGE;
	}
	
	/**
	 * @param attribute
	 * @description   额外的处理方法
	 */
	public  void endInvoke(Map<String, Object> attribute){};
	/**
	 * @param attribute
	 * @description   额外的处理方法
	 */
	public  void endInvoke(){};
	
	public void setDynamicAttribute(String uri, String attribute, Object value)
			throws JspException {
		dynamicAttribute.put(attribute, value);

	}

	public abstract UIBean getUiBean();

	public abstract String getStartTemplate();

	public abstract String getEndTemplate();
	/**
	 * @return
	 * @description   验证父子关系的tag
	 */
	protected boolean isValid(){
		
		return true;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the renderTo
	 */
	public String getRenderTo() {
		return renderTo;
	}

	/**
	 * @param renderTo
	 *            the renderTo to set
	 */
	public void setRenderTo(String renderTo) {
		this.renderTo = renderTo;
	}

}
